package cmd

import (
	"Sailor/app/Sailor-console-server/internal/consts"
	"Sailor/app/Sailor-console-server/internal/controller/api_login"
	"Sailor/app/Sailor-console-server/internal/controller/api_setting_api"
	"Sailor/app/Sailor-console-server/internal/controller/api_setting_menu"
	"Sailor/app/Sailor-console-server/internal/controller/api_setting_role"
	"Sailor/app/Sailor-console-server/internal/controller/api_setting_user"
	"Sailor/app/Sailor-console-server/internal/controller/api_task"
	"Sailor/app/Sailor-console-server/internal/controller/api_task_log"
	"Sailor/app/Sailor-console-server/internal/controller/api_user_menu"
	"Sailor/app/Sailor-console-server/internal/middleware"
	"Sailor/app/Sailor-console-server/utility/utility_task"
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {

			// 初始化定时任务
			utility_task.Initialize(ctx)

			consts.Server = g.Server()
			// 不鉴权路由
			consts.Server.Group("/api", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Middleware(ghttp.MiddlewareCORS)
				group.Bind(
					api_login.NewV1(),
				)
			})
			// 鉴权路由
			consts.Server.Group("/api/auth", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Middleware(ghttp.MiddlewareCORS)
				group.Middleware(middleware.AuthorityHandler)
				group.Bind(
					api_user_menu.NewV1(),
					api_setting_api.NewV1(),
					api_setting_menu.NewV1(),
					api_setting_role.NewV1(),
					api_setting_user.NewV1(),
					api_task.NewV1(),
					api_task_log.NewV1(),
				)
			})
			consts.Server.Run()
			return nil
		},
	}
)
